<?php
require 'include.php';
require 'config.php';

/*
 * 显示所有博文post
 * 显示博客信息blogInfo
 */
function showAll($num = '', $start = '')
{
	#初始化模板
	$views = new smasrty();
	$infoSQL = 'SELECT `BID` , `Blog_Title` , `Blog_Link` , `Blog_Feed` FROM blog_info';
	$newPostSQL = 'SELECT * FROM `post_into` ORDER BY `post_into`.`Pid` DESC LIMIT 0 , 30';
	$newPost = wt_SQL::get_all($newPostSQL);
	$blogInfo = wt_SQL::get_all($infoSQL);
	
	#加入未读文章数目
	foreach ($blogInfo as $key => $info)
	{
		$sql = "SELECT COUNT( `Pid` ) FROM `post_into` WHERE `Post_Feed` = '{$info['Blog_Feed']}' AND `Post_State` = '0'";
		$blogInfo[$key]['unreadNUM'] = wt_SQL::get_str($sql);
	}
	

	$title = '我的订阅'; 
	$views -> assign('title', $title);
	$views -> assign('blogInfo', $blogInfo);
	$views -> assign('newPost', $newPost);
	$views -> display('post.tpl');
}

function testF()
{
	$str = '要在要在要在要在要在要在要在要在要在要在要在要在要在要在要在要在';
	$views = new smasrty();
	$views -> assign('str', $str);
	$views -> display('test.tpl');
}

/*
 * 更新博文
 * 默认更新全部
 */
function updata($rss = '')
{
	if(empty($rss))
	{
		$sql = 'SELECT `Blog_Feed` FROM blog_info';
		updataAll($sql);
	}
	else
	{
		$rssPost = wt_xml::readRSS($rss);
		//print_r($rssPost['post']);
		wt_post::addPost($rssPost['post']);
	}
}

function updataAll($sql)
{
	$blogRss = wt_SQL::get_arr_num($sql);
	foreach ($blogRss as $rss)
	{
		$rssPost = wt_xml::readRSS($rss);
		wt_post::addPost($rssPost['post']);
	}
}
/*
 * 显示博客信息
 */
function blogInfo()
{
	$sql = 'SELECT `BID` , `Blog_Title` , `Blog_Link` , `Blog_Feed` FROM blog_info';
	$blogInfo = wt_SQL::get_all($sql);
}

/*
 * 搜索相关
 * $key：关键字
 * $post：搜索结果的返回值
 */
function search($key = '', $start = 0,  $num = 10)
{	
/*
	if ($start > 1)
	{
		$start = ($start - 1) * 10;
	}
	else
	{
		$start = 0;
	}*/
	// 获取总数据量
	$sqlCount = 'SELECT count( * ) AS amount FROM `post_into` WHERE `Post_Content` LIKE \'%' . $key . '%\'ORDER BY `Post_PubDate` DESC';
	$count = wt_SQL::get_one($sqlCount);
	$conutNum = $count['amount'];
	#生成URL
	$url = '?action=' . __FUNCTION__ . '&key=' . $key . '&';
	$page = pageF($conutNum, $url);
	
	#初始化模板
	$views = new smasrty();
	$post = wt_search::so($key, $num, $start);
	//print_r($post);
	$title = '搜索_' . $key;
	$views -> assign('key', $key);
	$views -> assign('title', $title);
	$views -> assign('post', $post);
	$views -> assign('page', $page);
	$views -> assign('conutNum', $conutNum);
	$views -> display('main.tpl');
}



/*
 * 分页
 */
function page($post, $url='')
{
	echo count($post);
}
/**
 * 读取某一篇日志
 */
function readOne($id)
{
	#初始化模板
	$views = new smasrty();
	$sql = 'SELECT * FROM `post_into` WHERE `Pid` = \'' . $id . '\'';
	$updateSQL = 'UPDATE `feed`.`post_into` SET `Post_State` = \'1\' WHERE `post_into`.`Pid` =\'' . $id . '\'';
	mysql_query($updateSQL);
	$post = wt_SQL::get_one($sql);
	//echo $sql;
	//print_r($post);
	$views -> assign('title', $post['Post_Title']);
	$views -> assign('Post_Link', $post['Post_Link']);
	$views -> assign('Post_PubDate', $post['Post_PubDate']);
	$views -> assign('Post_Content', $post['Post_Content']);
	$views -> assign('Post_Creator', $post['Post_Creator']);
	$views -> display('readpost.tpl');
}
/*
 * 读取源下所有日志
 */
function readRSS($rss, $state = NULL, $start = 0, $num = 10)
{
	
	if ($state != NULL)
	{
		$sql = 'SELECT * FROM `post_into` WHERE `Post_Feed` = \'' . $rss . '\' AND `Post_State` =' . $state . ' ORDER BY `Post_PubDate` DESC ';
		$stateStr = '&state=' . $state;
	}
	else
	{
		$sql = 'SELECT * FROM `post_into` WHERE `Post_Feed` = \'' . $rss . '\'  ORDER BY `Post_PubDate` DESC ';
		$stateStr = '&state';
	}
	$unreadSQL = 'SELECT * FROM `post_into` WHERE `Post_Feed` = \'' . $rss . '\'  ORDER AND `Post_State` = \'0\' BY `Post_PubDate` DESC';
	$sqlTitle = 'SELECT `Blog_Title` FROM `blog_info` WHERE `Blog_Feed` = \'' . $rss . ' \' LIMIT 1';
	$info = wt_SQL::get_one($sqlTitle);
	// 获取总数据量
	$conutNum = wt_SQL::select_query_num($sql);
	#生成URL
	$url = '?action=' . __FUNCTION__ . '&url=' . $rss . $stateStr . '&';
	$page = pageF($conutNum, $url);
	readPost($sql, $info['Blog_Title'], $num, $start, $page, $conutNum);
}

/**
 * 返回作者下的所有文章
 * @param string $creator
 * @param int $start
 * @param int $num
 * 
 * @return void
 */
function readCreator($creator, $start = 0, $num = 10)
{
	$sql = 'SELECT * FROM `post_into` WHERE `Post_Creator` = \'' . $creator . '\' ORDER BY `post_into`.`Post_PubDate` DESC';
	// 获取总数据量
	$conutNum = wt_SQL::select_query_num($sql);
	$title = '搜索_' . $creator;
	#生成URL
	$url = '?action=' . __FUNCTION__ . '&creator=' . $creator;
	$page = pageF($conutNum, $url);
	readPost($sql, $title, $num , $start, $page, $conutNum);
}


/*
 * 查看文章
 */
function readPost($sql, $title, $num, $start , $pager = '', $conutNum)
{
	#初始化模板
	$views = new smasrty();
	#页码转换
	if ($start == 1)
	{
		$start = 0;
	}
	$post = wt_SQL::selectLimit($sql, $num, $start);
	
	
	//print_r($post);
	$views -> assign('post', $post);
	$views -> assign('title', $title);
	$views -> assign('pager', $pager);
	$views -> assign('countNUM', $conutNum);
	$views -> display('main.tpl');
}

/*
 * 测试更新文章
 */
function d()
{
	$sql = 'SELECT * FROM `post_into`';
	$post = wt_SQL::selectLimit($sql);
	wt_post::getUnreadArticles($post);
}

/**
 * 哥是分页
 * @access	public
 * @param	int			$conutNum		记录数总量
 * @param	string		$url			传入的URL
 * @param	int			$PageSize		每页记录数，默认为10
 * 
 * @return	string		$page_string
 */
function pageF($conutNum, $url, $PageSize = 10)
{
	
	$page = http::get('page');
	if ($page == 1)
	{
		$page = 0;
	}
	

	if ($conutNum)
	{
		//如果总数据量小于$PageSize，那么只有一页
		if ($conutNum <= $PageSize)
		{
			$page_count = 1;
		}
		
   		if ($conutNum % $PageSize)
   		{
   			//取总数据量除以每页数的余数
   			//如果有余数，则页数等于总数据量除以每页数的结果取整再加一
   			$page_count = (int)($conutNum / $PageSize) + 1;           
   		}
   		else
   		{
   			//如果没有余数，则页数等于总数据量除以每页数的结果
   			$page_count = $conutNum / $PageSize;                      
   		}
	}
	else
	{
		$page_count = 0;
	}

	// 翻页链接
	$page_string = '';
	
	if ($page == 1 || $page == 0)
	{
		$page_string .= '第一页|上一页|';
	}
	else
	{
		$page_string .= '<a href="show.php' . $url . 'page=1">第一页</a>|<a href="show.php' . $url . 'page=' . ($page - $PageSize) . '">上一页</a>|';
	}
	
	if ((($page + $PageSize) >= $conutNum) || ($page_count == 0))
	{
		$page_string .= '下一页|尾页';
	}
	else
	{
		$end = ($page_count-1) * $PageSize;//页尾数
		$page_string .= '<a href="show.php' . $url .'page=' . ($page + $PageSize) .'">下一页</a>|<a href="show.php' . $url . 'page=' . $end .'">尾页</a>';
	}

	return $page_string;
}
wt_method::get($_GET);
